package ysf.com.listnode;

/**
 * 创建一个链表
 *
 * @author ysf
 * @date 2021/7/10 13:04
 */
public class ListNode {


    public int val;   // 数值 data
    public ListNode next;  // 结点node

    public ListNode(int x) {
        val = x;
    }

    /**
     * 添加新的结点
     *
     * @param newval
     */
    public void add(int newval) {
        ListNode newNode = new ListNode(newval);
        if (this.next == null) {
            this.next = newNode;
        } else {
            this.next.add(newval);
        }
    }

    /**
     * 打印链表
     */
    public void print() {
        System.out.print(this.val);
        if (this.next != null) {
            System.out.print("->");
            this.next.print();
        }
        System.out.println();
    }

}
